package com.wc.alorithm_blue_bridge._动态规划.线性DP.舞狮表演;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2025/2/14 14:43
 * @description
 * https://www.lanqiao.cn/problems/20181/learning/?contest_id=233
 */
public class Main {
    /**
     * 思路：
     * 只能从上面走, 或者从左边走到当前位置
     * 需要判断左边走否可达
     * 判断上边是否可达
     * 上述判断其f[i][j] == INF
     * 判断当前位置是否可达 左边走, 同奇偶性
     * 上边走 当前位置偶数 +n, 奇数免费
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 1010, INF = 0x3f3f3f3f;
    static int[][] a = new int[N][N], f = new int[N][N];
    static int n;

    public static void main(String[] args) {
       int T = sc.nextInt();
       while (T-- > 0) {
           n = sc.nextInt();
           for (int i = 1; i <= n; i++) {
               for (int j = 1; j <= n; j++) {
                   a[i][j] = sc.nextInt();
                   f[i][j] = INF;
               }
           }
           f[1][1] = 0;
           if ((a[1][1] & 1) == 0) f[1][1] = n;
           for (int i = 1; i <= n; i++) {
               for (int j = 1; j <= n; j++) {
                   // 从左边走
                   if (j - 1 >= 1 && f[i][j - 1] != INF && (a[i][j] + a[i][j - 1]) % 2 == 0) {
                       f[i][j] = Math.min(f[i][j], f[i][j - 1]);
                   }

                   // 从上面走
                   if (i - 1 >= 1 && f[i - 1][j] != INF) {
                       if (a[i][j] % 2 == 1) f[i][j] = Math.min(f[i][j], f[i - 1][j]);
                       else f[i][j] = Math.min(f[i][j], f[i - 1][j] + n);
                   }

                   // 往右走不通条件
                   if (f[i][j] == INF) break;
               }
           }
           if (f[n][n] == INF) out.println("NO!");
           else out.println(f[n][n]);
       }

       out.flush();
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}

